package com.example.beautymakeup.controller;

import com.example.beautymakeup.dao.ShopDao;
import com.example.beautymakeup.domain.Code;
import com.example.beautymakeup.domain.PageBean;
import com.example.beautymakeup.domain.Result;
import com.example.beautymakeup.pojo.Shop;
import com.example.beautymakeup.service.ShopService;
import com.example.beautymakeup.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping("/{uid}")
    public Result getByUid(@RequestHeader("Shop_token") String token,@PathVariable("uid") int uid) throws Exception {
        Shop byUid = shopService.getByUid(token, uid);
        if (byUid != null){
            return new Result(Code.GET_OK,"获取成功",byUid);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping
    public Result get(@RequestHeader("Shop_token") String token, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Shop> shopPageBean = shopService.get(token, page, size);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/all")
    public Result getAll(@RequestHeader("Shop_token") String token, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Shop> shopPageBean = shopService.getAll(token, page, size);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/admin/all")
    public Result getByTypeId(@RequestHeader("Shop_token") String token,@RequestParam("classifyId") int classifyId) throws Exception {
        PageBean<Shop> shopPageBean = shopService.getByTypeId(token, classifyId);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @PutMapping("/update")
    public Result update(@RequestHeader("Shop_token") String token, @RequestBody Shop shop) throws Exception {
        boolean update = shopService.update(token, shop);
        if (update){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @PutMapping("/type")
    public Result updateType(@RequestHeader("Shop_token") String token, @RequestBody Shop shop) throws Exception {
        boolean update = shopService.updateType(token, shop);
        if (update){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_ERR,"修改失败");
    }

    @PostMapping("/add")
    public Result add(@RequestHeader("Shop_token") String token, @RequestBody Shop shop) throws Exception {
        boolean add = shopService.add(token, shop);
        if (add){
            return new Result(Code.SAVE_OK,"新增成功");
        }
        return new Result(Code.SAVE_ERR,"新增失败");
    }

    @PutMapping("/ban")
    public Result ban(@RequestHeader("Shop_token") String token,@RequestBody Shop shop) throws Exception {
        boolean ban = shopService.ban(token, shop);
        if (ban){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_OK,"修改失败");
    }

    @DeleteMapping
    public Result delete( @RequestHeader("Shop_token") String token, @RequestBody Shop shop) throws Exception {
        boolean delete = shopService.delete(token, shop);
        if (delete){
            return new Result(Code.DELETE_OK,"删除成功");
        }
        return new Result(Code.DELETE_ERR,"删除失败");
    }

    @PutMapping("/hot")
    public Result hot(@RequestHeader("Shop_token") String token, @RequestBody Shop shop) throws Exception {
        boolean hot = shopService.hot(token, shop);
        if (hot){
            return new Result(Code.UPDATE_OK,"修改成功");
        }
        return new Result(Code.UPDATE_OK,"修改失败");
    }

    @GetMapping("/hot")
    public Result getHotShop(@RequestHeader("Shop_token") String token,@RequestParam("hotId") int hotId, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Shop> shopPageBean = shopService.getHotShop(token, hotId,page, size);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/type")
    public Result getTypeShop(@RequestHeader("Shop_token") String token,@RequestParam("classifyId") int classifyId, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Shop> shopPageBean = shopService.getTypeShop(token, classifyId,page, size);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/search")
    public Result search(@RequestHeader("Shop_token") String token,@RequestParam("shopName") String shopName, @RequestParam("page") int page, @RequestParam("size") int size) throws Exception {
        PageBean<Shop> shopPageBean = shopService.search(token, shopName,page, size);
        if (shopPageBean != null){
            return new Result(Code.GET_OK,"获取成功",shopPageBean);
        }
        return new Result(Code.GET_ERR,"获取失败");
    }

    @GetMapping("/count/{uid}")
    public Result getShopCOunt(@RequestHeader("Shop_token") String token,@PathVariable("uid") int uid) throws Exception {
        int shopCount = shopService.getShopCount(token, uid);
        return new Result(Code.GET_OK,"获取成功",shopCount);
    }
}
